package MapEditor.Editor
{
	import Engine.Module.ModuleBase;
	
	import Interface.UIControl.IButton;
	import Interface.UIControl.ITextBase;
	
	import MapEditor.MapEditor;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	
	public class Editor extends ModuleBase
	{
		
		private var _widthTxt:ITextBase = null;
		private var _onConfiremBtn:IButton = null;
		private var _bgBtn:IButton = null;
		private var _parent:MapEditor = null;
		private var _map:Map = null;
		private var _myBgFileReference:FileReference = null;
		private var _imagesFilter:FileFilter = new FileFilter("图片", "*.jpg;*.gif;*.png");
		private var _mapsaveFileReference:FileReference = null;
		private var _eventEditor:EventEditor = null;
		
		public function Editor(p_content:Sprite,_parent:MapEditor)
		{
			super(p_content);
			this._parent = _parent;
			this._onConfiremBtn = this._parent.ControlMgr.getButton(this._content["COMFIREM"])
			this._onConfiremBtn.setOnClickFun(onConfirem);
			this._bgBtn = this._parent.ControlMgr.getButton(this._content["BACK_GROUND"])
			this._bgBtn.setOnClickFun(onSetBackBg);
			this._bgBtn.setDisable();
			this._parent.ControlMgr.getButton(this._content["SAVE"]).setOnClickFun(save);
			
			this._widthTxt = this._parent.ControlMgr.getText(this._content["MAP_WIDTH"]);
			this._widthTxt.setInputText(true);
			this._widthTxt.textField.restrict = "0-9";
			
			
			this._myBgFileReference = new FileReference();
			this._myBgFileReference.addEventListener(Event.SELECT,onSelectBg);
			this._myBgFileReference.addEventListener(Event.COMPLETE,onLoadBgCompulete);
			
			this._mapsaveFileReference = new FileReference();
			this._mapsaveFileReference.addEventListener(Event.SELECT,onSelectSave);
			this._mapsaveFileReference.addEventListener(Event.COMPLETE,onSaveCompulete);
			
			this._eventEditor = new EventEditor(this._content["NPC_INSERT"],this._parent);
		}
		
		private function onConfirem():void
		{
			this._onConfiremBtn.setDisable();
			this._widthTxt.textField.mouseEnabled = false;
			this._map = new Map(int(this._widthTxt.text));
			this._map.setDouClickFun(this._eventEditor.onShow);
			this.addChild(this._map);
			this.addChild(this._eventEditor.content);
			this._bgBtn.setAble();
		}
		
		private function save():void
		{
			var xml:XML = new XML("<root></root>");
			xml.appendChild(XML("<q111>text</q111>"))
			this._mapsaveFileReference.save(xml,"aaa.xml");
		}
		
		private function onSetBackBg():void
		{
			this._myBgFileReference.browse([this._imagesFilter]);
		}
		
		private function onSelectBg(e:Event):void
		{
			var fileReference:FileReference = e.target as FileReference;
			fileReference.load();
		}
		
		private function onSelectSave(e:Event):void
		{
			var fileReference:FileReference = e.target as FileReference;
		}
		
		private function onSaveCompulete(e:Event):void
		{
			trace("save complete");
		}
		
		
		private function onLoadBgCompulete(e:Event):void
		{
			var ba:ByteArray = e.target.data as ByteArray
			var _loader:Loader =new Loader ;
			_loader.loadBytes(ba);
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadBgCompulete);
		}
		
		private function loadBgCompulete(e:Event):void
		{
			var bitmap:Bitmap = e.target.content as Bitmap;
			this._map.addChildAt(bitmap,0);
		}
		
		
	}
}